using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.WinApp.Utils;
using System.Data;
using InfoExpress.HIV.Domains;
using InfoExpress.HIV.WinApp.Business;
using InfoExpress.HIV.Services;

namespace InfoExpress.HIV.WinApp.Forms
{
  public partial class FrmTiepNhanBenhNhan : XtraForm
  {
    #region Members and Properties

    private DataTable _dtbDsBnChoTiepNhan;
    private DataTable _dtbDsBnTuDo;
    private TList<UtDmdv> _donViList;

    #endregion

    public FrmTiepNhanBenhNhan()
    {
      InitializeComponent();
    }

    #region Event Handlers

    private void FrmTiepNhanBenhNhan_Load(object sender, EventArgs e)
    {
      LoadData();
    }

    #region Command Handlers

    private void btnOK_1_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        DataRow dataRow = gvDsBenhNhan.GetFocusedDataRow();
        if (TiepNhanDieuTri(dataRow["MaBenhNhan"].ToString()))
        {
          gvDsBenhNhan.DeleteRow(gvDsBenhNhan.FocusedRowHandle);
        }
      }
    }

    private void btnReject_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhan.FocusedRowHandle >= 0)
      {
        DataRow dataRow = gvDsBenhNhan.GetFocusedDataRow();
        ChuyenOpcBuffer opcBuffer =
          GlobalCache.ChuyenOpcBufferService.GetByBenhNhanId((int)dataRow["BenhNhanId"]);

        opcBuffer.ToiOpc = null;
        GlobalCache.ChuyenOpcBufferService.Save(opcBuffer);
        gvDsBenhNhan.DeleteRow(gvDsBenhNhan.FocusedRowHandle);
      }
    }

    private void btnOK_2_Click(object sender, EventArgs e)
    {
      if (gvDsBenhNhanTuDo.FocusedRowHandle >= 0)
      {
        DataRow dataRow = gvDsBenhNhanTuDo.GetFocusedDataRow();
        if (TiepNhanDieuTri(dataRow["MaBenhNhan"].ToString()))
        {
          gvDsBenhNhanTuDo.DeleteRow(gvDsBenhNhanTuDo.FocusedRowHandle);
        }
      }
    }

    #endregion

    #region Data Loading

    private void bwDataLoading_DoWork(object sender, DoWorkEventArgs e)
    {
      try
      {
        DataSet dtsBnChoTiepNhan = GlobalCache.ChuyenOpcBufferService.DsBenhNhan(GlobalCache.Permission.UnitCode, false);
        DataSet dtsBnTuDo = GlobalCache.ChuyenOpcBufferService.DsBenhNhan(GlobalCache.Permission.UnitCode, true);

        _dtbDsBnChoTiepNhan = dtsBnChoTiepNhan.Tables[0];
        _dtbDsBnTuDo = dtsBnTuDo.Tables[0];
      }
      catch (Exception ex)
      {
        _dtbDsBnChoTiepNhan = null;
        _dtbDsBnTuDo = null;
        Logger.LogCritical("FrmTiepNhanBenhNhan::bwDataLoading_DoWork", ex, "General", null);
      }
    }

    private void bwDataLoading_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      Cursor = Cursors.Default;
      MainFormUtils.StopProgress();

      if (_dtbDsBnChoTiepNhan != null && _dtbDsBnTuDo != null)
      {
        gcDsBenhNhan.DataSource = _dtbDsBnChoTiepNhan;
        gcDsBenhNhanTuDo.DataSource = _dtbDsBnTuDo;
      }
      else
      {
        MainFormUtils.SetLoadedStatus();
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #endregion DataLoading BackgroundWorker

    #endregion

    #region Helper Methods

    private void BindData()
    {
      _donViList = GlobalCache.DonViService.GetAll();
      rilueDonVi.DataSource = _donViList;
      rilueDonViTuDo.DataSource = _donViList;
    }

    private void LoadData()
    {
      Cursor = Cursors.WaitCursor;
      MainFormUtils.StartProgress();
      MainFormUtils.SetLoadingStatus();
      bwDataLoading.RunWorkerAsync();
    }

    private bool TiepNhanDieuTri(string maBenhNhan)
    {
      bool isSuccessfully = true;
      try
      {
        #region Set thong tin dieu tri
        TList<DieuTri> dieuTriCuList = 
          GlobalCache.DieuTriService.LayDieuTriLanCuoi(maBenhNhan, null, true, true, null);

        DieuTri dieuTriCu = dieuTriCuList[0];
        DieuTri dieuTriMoi = new DieuTri();

        ChuyenOpcBuffer opcBuffer =
          GlobalCache.ChuyenOpcBufferService.GetByBenhNhanId(dieuTriCu.BenhNhanId);

        dieuTriMoi.BenhNhanId = dieuTriCu.BenhNhanId;
        dieuTriMoi.DieuTriArv = dieuTriCu.DieuTriArv;
        dieuTriMoi.NgayTiepNhanDieuTri = DateTime.Today;
        dieuTriMoi.UserTiepNhan = GlobalCache.Permission.Username;
        dieuTriMoi.MaOpc = GlobalCache.Permission.UnitCode;
        dieuTriMoi.DeleteFlag = false;

        dieuTriCu.MaChamDutDieuTri = (int)BenhNhanUtils.ChamDutDieuTri.ChuyenOpc;
        dieuTriCu.NgayChamDutDieuTri = opcBuffer.Ngay;
        #endregion

        #region Save/update lich su dieu tri
        int total = 0;
        TList<LichSuDieuTri> lsDieuTriList =
          GlobalCache.LichSuDieuTriService.GetPaged(
            String.Format("BenhNhanId={0}", dieuTriCu.BenhNhanId), "Id DESC", 0, 1, out total);

        if (total > 0)
        {
          LichSuDieuTri lsDieuTriCu = lsDieuTriList[0];

          LichSuDieuTri lsDieuTriMoi = new LichSuDieuTri { 
            BenhNhanId = lsDieuTriCu.BenhNhanId, 
            MaOpc = lsDieuTriCu.MaOpc, 
            NgayBatDau = lsDieuTriCu.NgayBatDau, 
            NgayDangKyArv = lsDieuTriCu.NgayDangKyArv, 
            TuOpc = lsDieuTriCu.TuOpc, 
            OpcGoc = lsDieuTriCu.OpcGoc, 
            MaKetThuc = (int)BenhNhanUtils.ChamDutDieuTri.ChuyenOpc, 
            NgayKetThuc = opcBuffer.Ngay };

          GlobalCache.LichSuDieuTriService.Save(lsDieuTriMoi);

          lsDieuTriMoi = new LichSuDieuTri
          {
            BenhNhanId = dieuTriCu.BenhNhanId,
            MaOpc = GlobalCache.Permission.UnitCode,
            NgayBatDau = DateTime.Today,
            TuOpc = lsDieuTriCu.MaOpc,
            OpcGoc = lsDieuTriCu.OpcGoc,
            NgayDangKyArv = lsDieuTriCu.NgayDangKyArv
          };

          GlobalCache.LichSuDieuTriService.Save(lsDieuTriMoi);
        }
        #endregion

        #region Save thong tin
        GlobalCache.DieuTriService.Save(dieuTriCu);
        GlobalCache.DieuTriService.Save(dieuTriMoi);
        GlobalCache.ChuyenOpcBufferService.Delete(opcBuffer);
        #endregion

        #region --For Non-Webservice--
        //For Non-Webservice
        //using (ConnectionScope.CreateTransaction())
        //{
        //  #region Set thong tin dieu tri
        //  DieuTri dieuTriCu = null;
        //  DieuTri dieuTriMoi = new DieuTri();

        //  ChuyenOpcBuffer opcBuffer =
        //    GlobalCache.ChuyenOpcBufferService.GetByBenhNhanId(dieuTriCu.BenhNhanId);

        //  dieuTriMoi.BenhNhanId = dieuTriCu.BenhNhanId;
        //  dieuTriMoi.DieuTriArv = dieuTriMoi.DieuTriArv;
        //  dieuTriMoi.NgayTiepNhanDieuTri = DateTime.Today;
        //  dieuTriMoi.UserTiepNhan = GlobalCache.Permission.Username;
        //  dieuTriMoi.MaOpc = GlobalCache.Permission.UnitCode;
        //  dieuTriMoi.DeleteFlag = false;

        //  dieuTriCu.MaChamDutDieuTri = (int)BenhNhanUtils.ChamDutDieuTri.ChuyenOpc;
        //  dieuTriCu.NgayChamDutDieuTri = opcBuffer.Ngay;
        //  #endregion

        //  #region Save/update lich su dieu tri
        //  int total = 0;
        //  TList<LichSuDieuTri> lsDieuTriList =
        //    GlobalCache.LichSuDieuTriService.GetPaged(
        //      String.Format("BenhNhanId={0}", dieuTriCu.BenhNhanId), "Id DESC", 0, 1, out total);

        //  if (total > 0)
        //  {
        //    LichSuDieuTri lsDieuTriCu = lsDieuTriList[0];

        //    lsDieuTriCu.MaKetThuc = (int)BenhNhanUtils.ChamDutDieuTri.ChuyenOpc;
        //    lsDieuTriCu.NgayKetThuc = opcBuffer.Ngay;

        //    LichSuDieuTri lsDieuTriMoi = new LichSuDieuTri { 
        //      BenhNhanId = dieuTriCu.BenhNhanId, 
        //      MaOpc = GlobalCache.Permission.UnitCode, 
        //      NgayBatDau = DateTime.Today, 
        //      TuOpc = lsDieuTriCu.MaOpc, 
        //      OpcGoc = lsDieuTriCu.OpcGoc, 
        //      NgayDangKyArv = lsDieuTriCu.NgayDangKyArv };

        //    GlobalCache.LichSuDieuTriService.Save(lsDieuTriCu);
        //    GlobalCache.LichSuDieuTriService.Save(lsDieuTriMoi);
        //  }
        //  #endregion

        //  #region Save thong tin
        //  GlobalCache.DieuTriService.Save(dieuTriCu);
        //  GlobalCache.DieuTriService.Save(dieuTriMoi);
        //  GlobalCache.ChuyenOpcBufferService.Delete(opcBuffer);
        //  #endregion

        //  if (ConnectionScope.Complete())
        //  {

        //  }
        //  else
        //  {
        //    isSuccessfully = false;
        //    UIUtils.ShowErrorMessage(Constants.MsgErrSave);
        //  }
        //}
        #endregion
      }
      catch (Exception ex)
      {
        isSuccessfully = false;
        Logger.LogCritical("FrmTiepNhanBenhNhan::TiepNhanDieuTri", ex, "General", null);
        UIUtils.ShowCannotSaveDataMessage();
      }

      return isSuccessfully;
    }

    #endregion
  }
}